package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 结算单控制器
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    @RequiresPermissions("system:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "deploy";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }

    //新增
    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo){
        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.updateInfo(bpmnInfo.getId(), bpmnInfo.getInfo());
        return AjaxResult.success();
    }


    //删除
    @RequiresPermissions("system:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult remove(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }

    //上传
    @RequiresPermissions("system:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file)  {
        if(file != null && file.getSize() > 0){
            //上传的文件名
            String filename = file.getOriginalFilename();
            //xxx.zip  -->zip
            String exp = FilenameUtils.getExtension(filename);
            if("zip".equalsIgnoreCase(exp)|| "bpmn".equalsIgnoreCase(exp)){
                String upload = null;
                try {
                    //上传成功之后，返回路径
                    upload = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                    System.err.println(upload);
                } catch (IOException e) {
                    e.printStackTrace();
                    return AjaxResult.error("流程文件上传失败");
                }
                //上传成功之后返回文件路径
                return AjaxResult.success("上传成功", upload);
            }else{
                return AjaxResult.error("流程定义文件仅支持 bpmn 和 zip 格式！");
            }
        }else{
            return AjaxResult.error("不允许上传空文件！");
        }
    }


    @RequiresPermissions("system:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String bpmnPath, String bpmnType, String info) throws FileNotFoundException {
        bpmnInfoService.deploy(bpmnPath, bpmnType, info);
        return AjaxResult.success();
    }



    @RequiresPermissions("system:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    public void  readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        //从activiti7数据库(流程部署/流程定义)获取流程文件/流程图片
        InputStream inputStream = bpmnInfoService.readResource(deploymentId, type);
        IOUtils.copy(inputStream, response.getOutputStream());
    }
}
